package com.hhhn.wk.main.tab2.view.xiangqing.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.main.tab2.view.xiangqing.bean.Evaluate;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.SysUtils;
import com.hhhn.wk.widget.customview.CircleImageView;
import com.hhhn.wk.widget.customview.MyGridView;
import com.hhhn.wk.widget.picture_viewer.ImagePagerActivity;

import java.util.ArrayList;
import java.util.List;

/**
 *@desc 分类列表页的adapter
 *@method 
 *@auth wenke
 *@date 2017-06-03 8:35
*/

public class Q2_List_Adapter extends BaseAdapter {
    private static final String TAG = "Q2_List_Adapter";

    private LayoutInflater mInflater;
    private BaseActivity activity;
    private List<Evaluate> list;

    private int wh;
    private Context context;
    public Q2_List_Adapter( Context context, BaseActivity activity, List<Evaluate> list) {
        super();
        this.mInflater = LayoutInflater.from(activity);
        this.activity = activity;
        this.context = context;
        this.wh=(SysUtils.getScreenWidth(activity)-SysUtils.Dp2Px(activity, 99))/3;
        this.list = list;
    }

    public List<Evaluate> getList() {
        return list;
    }

    public void setList(List<Evaluate> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list == null ? null : list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return list == null ? null : arg0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
//        Log.d("$$$$$$$$$$@@@@@@@@@@@@",""+list.size());
        if (list.size() == 0) {
            return null;
        }
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_evaluate, null);
            holder = new ViewHolder();

            holder.img = convertView.findViewById(R.id.img);//
            holder.name = convertView.findViewById(R.id.name);//
            holder.time = convertView.findViewById(R.id.time);//
            holder.content = convertView.findViewById(R.id.content);//
            holder.mRatingBar = convertView.findViewById(R.id.mRatingBar);//
            holder.gv_images = convertView.findViewById(R.id.gv_images);//
            holder.rl4 = convertView.findViewById(R.id.rl4);//



            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



        final Evaluate bean = list.get(position);

        String img = "";
        String imgG = "";
        String name = "";
        String time = "";
        String content = "";
        double mRatingBar = 0;


        if (bean != null) {
            img = bean.getMerchantsHeadPortrait();
            imgG = bean.getEvaluationImage();
            name = bean.getMerchantName();
            time = bean.getCmIndate();
            content = bean.getEvaluationContent();
            mRatingBar = bean.getEvaluationScore();

        }
        //加载网络头像
        AllPublic.showImg(activity,img,holder.img);
        //昵称
        holder.name.setText(name+"");
        holder.time.setText(time+"");
        holder.content.setText(content+"");
        holder.mRatingBar.setRating((float) mRatingBar);
        //是否含有图片
        if (imgG!=null&&!imgG.equals("")) {
            holder.rl4.setVisibility(View.VISIBLE);
            initInfoImages(holder.gv_images,imgG);
        } else {
            holder.rl4.setVisibility(View.GONE);
        }
        return convertView;
    }
    static class ViewHolder {
        CircleImageView img;
        TextView name;
        TextView time;
        TextView content;
        RatingBar mRatingBar;

        MyGridView gv_images;
        RelativeLayout rl4;
    }

    /**
     * 加载信息中包含的图片内容
     *
     *参数（GridView，图片路径）
     */
    public void initInfoImages(MyGridView gv_images, final String imgspath){
        Log.d("@@@@@@@@@",imgspath);
        /**
         * 加载图片多个网址   需要截取（必要时 用转义字符）
         *
         */
        if(imgspath!=null&&!imgspath.equals("")){
            String[] imgs=imgspath.split("\\|");
            final ArrayList<String> imageUrls=new ArrayList<String>();
            Log.d("@@@@@@@@@",""+imgs.length);
            if (imgs.length>3){
                for (int i = 0; i < 3; i++) {
//                Log.d("@@@@@@@@@", "" + imgs[i]);
                    imageUrls.add(imgs[i]);
                }
            }else {
                for (int i = 0; i < imgs.length; i++) {
//                Log.d("@@@@@@@@@", "" + imgs[i]);
                    imageUrls.add(imgs[i]);
                }
            }

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            gv_images.setLayoutParams(lp);

            ClassifyGridViewAdapter  nearByInfoImgsAdapter=new ClassifyGridViewAdapter(activity, imageUrls);
            gv_images.setAdapter(nearByInfoImgsAdapter);
            /**
             * 查看大图
             */
            gv_images.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    imageBrower(position, imageUrls);
                    Log.d("传过去的网址","GG"+imageUrls);
//					Toast.makeText(context, "点击了第"+(position+1)+"张图片", Toast.LENGTH_LONG).show();
                }
            });
        }

    }
    /**
     * 打开图片查看器
     * @param position
     * @param urls2
     */
    protected void imageBrower(int position, ArrayList<String> urls2) {
        Intent intent = new Intent(context, ImagePagerActivity.class);
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        context.startActivity(intent);
    }
}
